import 'package:flutter/material.dart';
import '../../model/conversation.dart';
import '../../provider/chatdetailprovider.dart';

class ChatdetailBarUI extends StatefulWidget {
  ChatdetailBarUI({Key? key, required this.chatDetailProvider})
      : super(key: key);

  ChatDetailProvider chatDetailProvider;

  @override
  _ChatdetailBarUIState createState() => _ChatdetailBarUIState();
}

class _ChatdetailBarUIState extends State<ChatdetailBarUI> {

  bool isOnline =  false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();


    widget.chatDetailProvider.addListener(() {
      if(!this.mounted)
        return;

      setState(() {
        isOnline = widget.chatDetailProvider.toUserOnline;
      });

    });
  }

  @override
  Widget build(BuildContext context) {

    setState(() {
      isOnline =  widget.chatDetailProvider.toUserOnline;
    });

    return Container(
      padding: EdgeInsets.only(right: 16),
      child: Row(
        children: <Widget>[
          SizedBox(
            width: 2,
          ),
          CircleAvatar(
            backgroundImage:
                AssetImage(widget.chatDetailProvider.conversation.avatar),
            maxRadius: 20,
          ),
          SizedBox(
            width: 12,
          ),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  this.widget.chatDetailProvider.conversation.title,
                  style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600),
                ),
                SizedBox(
                  height: 6,
                ),
                Text(
                  isOnline?"在线":"离线",
                  style: TextStyle(color: Colors.grey.shade600, fontSize: 13),
                ),
              ],
            ),
          ),
          Icon(
            Icons.settings,
            color: Colors.black54,
          ),
        ],
      ),
    );
  }
}
